package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 17/1/3.
 */

public class JianzhengrenAdapter extends BaseAdapter{
    private List<EatShowUserInfo> objects = new ArrayList<EatShowUserInfo>();

    private Context context;
    private LayoutInflater layoutInflater;
    private OnAdapterClickListener mListener;
    public interface OnAdapterClickListener{
        public void OnAdapterClick(EatShowUserInfo info);
    }

    public void setOnAdapterClickListener(OnAdapterClickListener listener){
        mListener = listener;
    }

    public JianzhengrenAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }



    public void updateData(List<EatShowUserInfo> list){
        objects.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData(){
        objects.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int size = objects.size();
        return size;
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
//        return 4;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.jianzhengren_item, parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }
        final EatShowUserInfo info = (EatShowUserInfo)getItem(position);
        initializeViews(info, (ViewHolder) convertView.getTag());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.OnAdapterClick(info);
                }
            }
        });
        return convertView;
    }

    private void initializeViews(EatShowUserInfo object, ViewHolder holder) {
        if (object != null){
            String url = object.getUserLogo();
            if (url != null && url.length()>0){
                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(context,40),ViewUtil.dip2px(context,40));
                String key = ImageCache.getInstance(context).getCacheKey(url,size);
                Bitmap cacheBitmap = ImageCache.getInstance(context).getBitmapFromMemoryCache(key);
                if (cacheBitmap != null && !cacheBitmap.isRecycled()){
                    holder.headImg.setImageBitmap(cacheBitmap);
                }else {
                    holder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(context).loadBitmapToView(holder.headImg, url, size);
                }
            }
            try {
                String nickName = URLDecoder.decode(object.getNickName(),"utf-8");
                holder.nickname.setText(nickName);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    protected class ViewHolder {
        private RoundImageView headImg;
        private TextView nickname;

        public ViewHolder(View view) {
            headImg = (RoundImageView) view.findViewById(R.id.head_img);
            nickname = (TextView) view.findViewById(R.id.nickname);
        }
    }
}
